<?php
/**
 * @since      File available since Release v1.1
 */

class discoverControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
        Tpl::output('index_sign','index');
        
	}
	
	/*
	 * 发现首页
	 */
	public function indexOp(){
					
		$model = Model();
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		
		$discoverid = $user['discoverid'];
		
		$condition = array();
		$condition['friend.uid'] = $_COOKIE['user_id'];
		$field = 'qunfa.id as id,qunfa.user_id as user_id';
		$list = $model->field($field)->table('qunfa,friend')->join('left')->on('qunfa.user_id = friend.fid')->where($condition)->order('qunfa.add_time desc')->select();
		
		$newsid = 0;
		if(!empty($list)){
			$newsid = $list[0]['id'];
		}
		if($newsid>$discoverid){
			$news_user = $model->table('user')->where(array('user_id'=>$list[0]['user_id']))->find();
			Tpl::output('news_user',$news_user);
		}
		
		Tpl::showpage('discover.index');
	}
	
	/*
	 * 新品开拍页面
	 */
	public function newsOp(){
		
		$model = Model();
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		
		$discoverid = $user['discoverid'];
		
		$condition = array();
		$condition['friend.uid'] = $_COOKIE['user_id'];
		$field = 'qunfa.id as id';
		$list = $model->field($field)->table('qunfa,friend')->join('left')->on('qunfa.user_id = friend.fid')->where($condition)->order('qunfa.add_time desc')->select();
		
		$newsid = 0;
		if(!empty($list)){
			$newsid = $list[0]['id'];
		}
		if($newsid>$discoverid){
			$model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->update(array('discoverid'=>$newsid));
		}
		
		Tpl::showpage('discover.news');
	}
	
	/*
	 * 新品开拍数据
	*/
	public function newsdataOp(){
		
		$model = Model();
		$pagesize = $this->pagesize;
		$pagenum = isset($_GET['nextpage'])?intval($_GET['nextpage']):1;
		if($pagenum == 1){
			$limit = "0,{$pagesize}";
		}else{
			$pagenum = ($pagenum-1)*$pagesize;
			$limit = "{$pagenum},{$pagesize}";
		}
		
		$condition = array();
		$condition['friend.uid'] = $_COOKIE['user_id'];
		$list = $model->table('qunfa,friend')->join('left')->on('qunfa.user_id = friend.fid')->where($condition)->order('qunfa.add_time desc')->limit($limit)->select();
		
		if(!empty($list)){
			foreach($list as $key=>$val){
				$condition = array();
				$condition['id'] = array('in',$val['auction_setid']);
				$auction_list = $model->table('auction')->where($condition)->select();
				$list[$key]['auction'] = $auction_list;
				$list[$key]['seller'] = $model->table('user')->where(array('user_id'=>$val['user_id']))->find();
			}
		}else{
			exit;
		}
		
		Tpl::output('list',$list);
		Tpl::showpage('discover.newsdata');
	}
	
}